<?php
	session_start();
	if(!isset($_SESSION["jara_admin"])) {
		header("Location: ../login.php");
		exit;
	}
	if($_SESSION["jara_permissions"][0] == 0) {	
		header("Location: error.php");
		exit;
	}
	require_once("../include/templating_fns.php");
	require_once("../include/db_fns.php");
	jara_page_start("Categories - Administrator CP");
	$result = jara_db_query("select * from jara_categories order by title");
	$num_cats = $result->num_rows;
?>
	<p><a href="add_category.php">Add a new category</a></p>
	<table border="1" class="admin-table">
		<tr>
			<th width="220"><strong>Title</strong></th>
			<th width="190"><strong>Name</strong></th>
			<th width="60"><strong>Actions</strong></th>
		</tr>
<?php
	if($num_cats == 0) {
?>
		<tr>
			<td colspan="4" align="center">There are no categories.</td>
		</tr>
<?php
	}
	for($i = 0; $i < $num_cats; $i++) {
		$row = $result->fetch_assoc();
?>
		<tr>
			<td><?php echo $row["title"]; ?></td>
			<td><?php echo $row["name"]; ?></td>
			<td>
				<a href="<?php echo jara_category_permalink($row["categoryid"]); ?>" title="View Posts in Category"><img src="images/jara_view.png" alt="View Posts in Category" border="0" style="border: none" /></a>
				<a href="edit_category.php?id=<?php echo $row["categoryid"]; ?>" title="Edit Category"><img src="images/jara_edit.png" alt="Edit Category" border="0" style="border: none" /></a>
				<a href="javascript:;" onclick="var q = confirm('Are you sure you want to delete this category?'); if(q) { window.location.href = 'delete_category.php?id=<?php echo $row["categoryid"]; ?>' }" title="Delete Category"><img src="images/jara_delete.png" alt="Delete Category" border="0" style="border: none" /></a>
			</td>
		</tr>
<?php
	}
?>
	</table>
<?php
	jara_page_end();
?>